﻿Imports System.Text
Imports System.Data
Imports System.ComponentModel
Imports Global_Visas_Business_Layer
Public Class frmAdminLetterPacks

    Private ReadOnly Property ClientType() As Integer
        Get
            If Me.radClientAll.IsChecked Then
                Return 0
            ElseIf Me.radClientPrivate.IsChecked Then
                Return 1
            ElseIf Me.radClientCorp.IsChecked Then
                Return 2
            End If
        End Get
    End Property

    Private ReadOnly Property SLSType() As Integer
        Get
            If Me.radShowAll.IsChecked Then
                Return 0
            ElseIf Me.radLimitTo.IsChecked Then
                If Me.drpLimitTo.SelectedIndex >= 0 Then
                    Return Me.drpLimitTo.SelectedValue
                Else
                    Return 0
                End If
            End If
        End Get
    End Property

    Private Sub AdminLetterPacks_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Loaded
        GeneralTasks.ChangeTheme(Me)
        GeneralTasks.EnumerateandFill(Me.LogicalChildren)

        With Me.drpLetterTypes
            .SelectedValuePath = "LetterType_Id"
            .DisplayMemberPath = "LetterType_Name"
            .ItemsSource = CType(Letters.LetterTypeList, IListSource).GetList
            .SelectedIndex = -1
        End With

        With Me.drpFromCustom
            .SelectedValuePath = "CaseStage_Title"
            .DisplayMemberPath = "CaseStage_Title"
            .ItemsSource = CType(Cases.CaseStageList, IListSource).GetList
            .SelectedIndex = -1
        End With

        With Me.drpToCustom
            .SelectedValuePath = "CaseStage_Title"
            .DisplayMemberPath = "CaseStage_Title"
            .ItemsSource = CType(Cases.CaseStageList, IListSource).GetList
            .SelectedIndex = -1
        End With

        With Me.drpLimitTo
            .SelectedValuePath = "SLSType_ID"
            .DisplayMemberPath = "SLSType_Name"
            .ItemsSource = CType(Letters.SLSTypeList, IListSource).GetList
            .SelectedIndex = -1
        End With

        Me.grdLetterPacks.ItemsSource = Letters.LetterPacks

        AddHandler grdLetterPacks.SelectionChanged, AddressOf grdLetterPacks_SelectedRowsChanged
        AddHandler radAll.Checked, AddressOf LetterTypeView_Changed
        AddHandler radFromList.Checked, AddressOf LetterTypeView_Changed
        AddHandler drpLetterTypes.SelectionChanged, AddressOf drpLetterTypes_SelectedIndexChanged

        AddHandler radFromCustom.Checked, AddressOf AvailableFrom_CheckedChanged
        AddHandler radFromLead.Checked, AddressOf AvailableFrom_CheckedChanged
        AddHandler radFromSold.Checked, AddressOf AvailableFrom_CheckedChanged

        AddHandler radToAfterClosed.Checked, AddressOf AvailableTo_CheckedChanged
        AddHandler radToClosed.Checked, AddressOf AvailableTo_CheckedChanged
        AddHandler radToCustom.Checked, AddressOf AvailableTo_CheckedChanged
        AddHandler radToSold.Checked, AddressOf AvailableTo_CheckedChanged

        AddHandler radLimitTo.Checked, AddressOf AvailableType_CheckedChanged
        AddHandler radShowAll.Checked, AddressOf AvailableType_CheckedChanged

        AddHandler drpLimitTo.SelectionChanged, AddressOf drpLimitTo_SelectedIndexChanged

    End Sub


    Private Sub grdLetterPacks_SelectedRowsChanged(ByVal sender As Object, ByVal e As System.EventArgs) 'Handles grdLetterPacks.SelectedRowsChanged
        Dim selectedLetterpack As DataRow = Me.grdLetterPacks.SelectedItem
        If selectedLetterpack Is Nothing Then
            Exit Sub
        End If

        DataBinding.BindListView(Letters.SLSList(selectedLetterpack(0)), Me.lvwSLS)
        DataBinding.BindListView(Letters.SLAAvailableList(selectedLetterpack(0), Me.SLSType), Me.lvwAvailable)
        Dim result As DataTable = Letters.LettersInPack(selectedLetterpack(0))
        Me.lvwLetters.ItemsSource = result

    End Sub

    Private Sub LetterTypeView_Changed(ByVal sender As Object, ByVal e As System.EventArgs)

        If Me.radAll.IsChecked Then
            Me.drpLetterTypes.IsEnabled = False
        Else
            Me.drpLetterTypes.IsEnabled = True
        End If

    End Sub

    Private Sub drpLetterTypes_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) 'Handles drpLetterTypes.SelectedIndexChanged
        If Me.drpLetterTypes.SelectedIndex > -1 Then
            Me.grdLetterPacks.ItemsSource = Letters.LetterPacks(Me.drpLetterTypes.SelectedValue)
        End If
    End Sub

    Private Function ClientTypes(ByVal ClientType As Integer) As DataTable

        Dim myTable As New DataTable
        Dim myCol As DataColumn
        Dim myRow As DataRow

        myCol = New DataColumn("id", System.Type.GetType("System.Int16"))
        myTable.Columns.Add(myCol)
        myCol = New DataColumn("text", System.Type.GetType("System.String"))
        myTable.Columns.Add(myCol)

        If ClientType = 0 Then
            myRow = myTable.NewRow
            myRow.Item("id") = "0"
            myRow.Item("text") = "All"
            myTable.Rows.Add(myRow)
        End If

        If ClientType = 0 Or ClientType = 1 Then
            myRow = myTable.NewRow
            myRow.Item("id") = "1"
            myRow.Item("text") = "Private"
            myTable.Rows.Add(myRow)
        End If

        If ClientType = 0 Or ClientType = 2 Then
            myRow = myTable.NewRow
            myRow.Item("id") = "2"
            myRow.Item("text") = "Corporate"
            myTable.Rows.Add(myRow)
        End If

        With Me.drpClientType
            .SelectedValuePath = "id"
            .DisplayMemberPath = "text"
            .ItemsSource = myTable
            .SelectedValue = ClientType
        End With


        Return myTable


    End Function

    Private Function OutputTypes(ByVal OutputType As Integer) As DataTable

        Dim myTable As New DataTable
        Dim myCol As DataColumn
        Dim myRow As DataRow

        myCol = New DataColumn("id", System.Type.GetType("System.Int16"))
        myTable.Columns.Add(myCol)
        myCol = New DataColumn("text", System.Type.GetType("System.String"))
        myTable.Columns.Add(myCol)

        If OutputType = 0 Then
            myRow = myTable.NewRow
            myRow.Item("id") = "0"
            myRow.Item("text") = "All"
            myTable.Rows.Add(myRow)
        End If

        If OutputType = 0 Or OutputType = 1 Then
            myRow = myTable.NewRow
            myRow.Item("id") = "1"
            myRow.Item("text") = "Print"
            myTable.Rows.Add(myRow)
        End If

        If OutputType = 0 Or OutputType = 2 Then
            myRow = myTable.NewRow
            myRow.Item("id") = "2"
            myRow.Item("text") = "Email"
            myTable.Rows.Add(myRow)
        End If

        If OutputType = 0 Or OutputType = 3 Then
            myRow = myTable.NewRow
            myRow.Item("id") = "3"
            myRow.Item("text") = "File"
            myTable.Rows.Add(myRow)
        End If

        If OutputType = 0 Or OutputType = 4 Then
            myRow = myTable.NewRow
            myRow.Item("id") = "4"
            myRow.Item("text") = "Print + Email"
            myTable.Rows.Add(myRow)
        End If

        If OutputType = 0 Or OutputType = 5 Then
            myRow = myTable.NewRow
            myRow.Item("id") = "5"
            myRow.Item("text") = "Print + File"
            myTable.Rows.Add(myRow)
        End If

        If OutputType = 0 Or OutputType = 6 Then
            myRow = myTable.NewRow
            myRow.Item("id") = "6"
            myRow.Item("text") = "Email + File"
            myTable.Rows.Add(myRow)
        End If


        Return myTable

    End Function

    Private Sub lvwLetters_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lvwLetters.SelectionChanged

        If Me.lvwLetters.SelectedItems.Count = 0 Then
            Me.btnAdd.IsEnabled = False
            Me.btnDelete.IsEnabled = False
            Exit Sub
        Else
            Me.btnAdd.IsEnabled = True
            Me.btnDelete.IsEnabled = True
        End If

        Dim myLetter As Letters.LetterInfo = Letters.GetLetter(Me.lvwLetters.SelectedItems(0).SubItems(1).Text)

        If myLetter.LetterId = 0 Then
            Exit Sub
        End If

        Select Case myLetter.CustomerType
            Case 0
                'All
                Me.ClientTypes(0)
            Case 1
                'Private
                Me.ClientTypes(1)
            Case 2
                'Corporate
                Me.ClientTypes(2)

        End Select

        Select Case myLetter.OutputType

            Case 0
                'Nothing to switch off
                Me.OutputTypes(0)

            Case 1
                'Switch off non-print options
                Me.OutputTypes(1)

            Case 2
                'Switch off non-email options
                Me.OutputTypes(2)

            Case 3
                'Switch off non-file options
                Me.OutputTypes(3)

            Case 4
                'Switch off file options
                Me.OutputTypes(4)

            Case 5
                'Switch off email options
                Me.OutputTypes(5)


            Case 6
                'Switch off print options
                Me.OutputTypes(6)


        End Select





    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        '    frmAdminAddLetter.LetterPackId = CType(Me.grdLetterPacks.SelectedRows(0), Xceed.Grid.DataRow).Cells(0).Value
        '    frmAdminAddLetter.ShowDialog()
        '    DataBinding.BindListView(Letters.LettersInPack(CType(Me.grdLetterPacks.SelectedRows(0), Xceed.Grid.DataRow).Cells(0).Value), Me.lvwLetters)
    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        Dim selectedLetterPack As DataRow = Me.grdLetterPacks.SelectedItem
        If selectedLetterPack Is Nothing Then
            Exit Sub
        End If

        If MsgBox("Are you sure you want to remove this letter from the letterpack?", vbYesNo + vbQuestion, "Confirm Delete") <> MsgBoxResult.Yes Then
            Exit Sub
        End If

        If Letters.DeleteLetterFromPack(Me.lvwLetters.SelectedItems(0)(0), Me.lvwLetters.SelectedItems(0)(1), Me.lvwLetters.SelectedItems(0)(2), selectedLetterPack(0), selectedLetterPack(1)) Then
            Dim result As DataTable = Letters.LettersInPack(selectedLetterPack(0))
            Me.lvwLetters.ItemsSource = result
        End If


    End Sub

    Private Sub btnDeleteLetterpack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDeleteLetterpack.Click
        Dim selectedLetterPack As DataRow = Me.grdLetterPacks.SelectedItem
        If selectedLetterPack Is Nothing Then
            Exit Sub
        End If

        Dim c As Integer = Letters.StockLetterUsage(selectedLetterPack(0))

        If c = 0 Then
            'Error
            MsgBox("Cant verify usage", MsgBoxStyle.Critical, "Error")
            Exit Sub
        End If

        If c = 0 Then
            If MsgBox("Are you sure you want to delete this Letterpack?", vbYesNo + vbQuestion, "Confirm Delete") <> MsgBoxResult.Yes Then
                Exit Sub
            End If
        Else
            If MsgBox("This Letterpack is in use in " & c & " Stock Letter Sets, are you sure you want to delete it?", vbYesNo + vbQuestion, "Confirm Delete") <> MsgBoxResult.Yes Then
                Exit Sub
            End If
        End If

        If Letters.DeleteLetterPack(selectedLetterPack(0), selectedLetterPack(1), c) Then
            Me.grdLetterPacks.ItemsSource = Letters.LetterPacks
        End If

    End Sub

    Private Sub btnUp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUp.Click
        Dim selectedLetters As DataRowView = Me.lvwLetters.SelectedItem
        If selectedLetters Is Nothing Then
            Exit Sub
        End If
        Dim selectedLetterPacks As DataRow = Me.grdLetterPacks.SelectedItem

        Dim PrintOrder As Integer = Letters.LetterLinkPrintOrder(Me.lvwLetters.SelectedItems(0).SubItems(0).Text)

        'is this the first record?
        If PrintOrder = 1 Then Exit Sub

        If Letters.UpdatePrintOrder(selectedLetters(0).Text, selectedLetterPacks(0), PrintOrder, -1) Then
            Dim result As DataTable = Letters.LettersInPack(selectedLetterPacks(0))
            Me.lvwLetters.ItemsSource = result
        End If

    End Sub

    Private Sub btnDown_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDown.Click

        Dim selectedLetters As DataRowView = Me.lvwLetters.SelectedItem
        If selectedLetters Is Nothing Then
        End If
        Dim selectedLetterPacks As DataRow = Me.grdLetterPacks.SelectedItem

        Dim PrintOrder As Integer = Letters.LetterLinkPrintOrder(Me.lvwLetters.SelectedItems(0).SubItems(0).Text)


        'is this the last record?

        If Me.lvwLetters.SelectedItems(0).Index = Me.lvwLetters.Items.Count - 1 Then
            Exit Sub
        End If

        If Letters.UpdatePrintOrder(selectedLetters(0), selectedLetterPacks(0), PrintOrder, 1) Then
            Dim result As DataTable = Letters.LettersInPack(selectedLetterPacks(0))
            Me.lvwLetters.ItemsSource = result
        End If

    End Sub

    Private Sub btnRemoveFromSLS_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemoveFromSLS.Click
        Dim selectedSLS As DataRowView = Me.lvwSLS.SelectedItem
        If selectedSLS Is Nothing Then
            MsgBox("Please select which SLS to remove the Letterpack from", MsgBoxStyle.Exclamation, "Error")
            Exit Sub
        End If
        Dim selectedLetterPacks As DataRow = Me.grdLetterPacks.SelectedItem
        If Letters.RemoveSLSLink(Me.lvwSLS.SelectedItems(0).SubItems(4).Text) Then
            Dim resultSLS As DataTable = Letters.SLSList(selectedLetterPacks(0))
            DataBinding.BindListView(resultSLS, Me.lvwSLS)
            Dim resultAvailable As DataTable = Letters.SLAAvailableList(selectedLetterPacks(0), Me.SLSType)
            DataBinding.BindListView(resultAvailable, Me.lvwAvailable)
        End If

    End Sub

    Private Sub lvwAvailable_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If Me.lvwAvailable.SelectedItems.Count > 0 Then
            Me.btnAddToSLS.IsEnabled = True
        Else
            Me.btnAddToSLS.IsEnabled = False
        End If
    End Sub

    Private Sub btnAddToSLS_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddToSLS.Click

        Dim StageStart As Integer
        Dim StageEnd As Integer
        Dim selectedLetterPacks As DataRow = Me.grdLetterPacks.SelectedItem
        If selectedLetterPacks Is Nothing Then
            MsgBox("No Letterpack selected", MsgBoxStyle.Exclamation, "Error")
            Exit Sub
        End If

        If Me.lvwAvailable.SelectedItems.Count = 0 Then
            MsgBox("No Stock Letter Sets selected", MsgBoxStyle.Exclamation, "Error")
            Exit Sub
        End If

        If Me.radFromLead.IsChecked Then
            StageStart = 0
        ElseIf Me.radFromSold.IsChecked Then
            StageStart = 100
        ElseIf Me.radFromCustom.IsChecked Then

            If Me.drpFromCustom.SelectedIndex = -1 Then
                MsgBox("No Start Stage selected", MsgBoxStyle.Exclamation, "Error")
                Me.drpFromCustom.Focus()
                Exit Sub
            End If

            StageStart = Me.drpFromCustom.SelectedValue

        End If

        If Me.radToSold.IsChecked Then
            StageEnd = 99
        ElseIf Me.radToClosed.IsChecked Then
            StageEnd = 999
        ElseIf Me.radToAfterClosed.IsChecked Then
            StageEnd = 0
        ElseIf Me.radToCustom.IsChecked Then

            If Me.drpToCustom.SelectedIndex = -1 Then
                MsgBox("No End Stage selected", MsgBoxStyle.Exclamation, "Error")
                Me.drpToCustom.Focus()
                Exit Sub
            End If

            StageEnd = Me.drpToCustom.SelectedValue

        End If

        If StageEnd <> 0 And StageEnd < StageStart Then
            MsgBox("End Stage Cannot be before Start Stage", MsgBoxStyle.Exclamation, "Error")
            Exit Sub
        End If

        'Dim lvi As ListViewItem

        For count As Integer = 0 To Me.lvwAvailable.Items.Count
            Dim item As Windows.Controls.ItemsControl = Me.lvwAvailable.Items(0)
            Letters.AddSLSLink(Me.lvwAvailable.Items(count)(0), selectedLetterPacks(0), 1, Me.ClientType, StageStart, StageEnd)
        Next
        'For Each lvi In Me.lvwAvailable.SelectedItems
        '    'Dim subitem As DataRowView = CType(lvi, DataRow)
        '    Letters.AddSLSLink(lvi(0).Text, selectedLetterPacks(0), 1, Me.ClientType, StageStart, StageEnd)
        'Next

        Dim resultSLS As DataTable = Letters.SLSList(selectedLetterPacks(0))
        'DataBinding.BindListView(Letters.SLSList(CType(Me.grdLetterPacks.SelectedRows(0), Xceed.Grid.DataRow).Cells(0).Value), Me.lvwSLS)
        DataBinding.BindListView(resultSLS, Me.lvwSLS)
        Dim resultAvailable As DataTable = Letters.SLAAvailableList(selectedLetterPacks(0), Me.SLSType)
        'DataBinding.BindListView(Letters.SLAAvailableList(CType(Me.grdLetterPacks.SelectedRows(0), Xceed.Grid.DataRow).Cells(0).Value, Me.SLSType), Me.lvwAvailable)
        DataBinding.BindListView(resultAvailable, Me.lvwAvailable)

    End Sub

    Private Sub AvailableFrom_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)

        If CType(sender, RadioButton).Tag = 3 Then
            Me.drpFromCustom.IsEnabled = True
        Else
            Me.drpFromCustom.IsEnabled = False
        End If

    End Sub

    Private Sub AvailableTo_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)

        If CType(sender, RadioButton).Tag = 4 Then
            Me.drpToCustom.IsEnabled = True
        Else
            Me.drpToCustom.IsEnabled = False
        End If

    End Sub

    Private Sub AvailableType_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)

        If CType(sender, RadioButton).Tag = 2 Then
            Me.drpLimitTo.IsEnabled = True
        Else
            Me.drpLimitTo.IsEnabled = False
        End If

    End Sub



    Private Sub btnAddToAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddToAll.Click

        Dim Msg As String = ""
        Dim StageStart As Integer
        Dim StageEnd As Integer

        Dim selectedLetterPacks As DataRow = Me.grdLetterPacks.SelectedItem

        If selectedLetterPacks Is Nothing Then
            MsgBox("No Letterpack selected", MsgBoxStyle.Exclamation, "Error")
            Exit Sub
        End If

        If Me.radShowAll.IsChecked Then
            Msg = "Are you sure you want to add this Letterpack to every Stock Letter Set on the System?"
        ElseIf Me.radLimitTo.IsChecked Then
            If Me.drpLimitTo.SelectedIndex = -1 Then
                MsgBox("No SLS Type selected, so unable to add", MsgBoxStyle.Exclamation, "Error")
                Me.drpLimitTo.Focus()
                Exit Sub
            End If

            Msg = "Are you sure you want to add this Letterpack to every " & Me.drpLimitTo.Text & " Stock Letter Set on the System?"
        End If

        If MsgBox(Msg, vbYesNo, "Confirm Add") <> MsgBoxResult.Yes Then Exit Sub


        If Me.radFromLead.IsChecked Then
            StageStart = 0
        ElseIf Me.radFromSold.IsChecked Then
            StageStart = 100
        ElseIf Me.radFromCustom.IsChecked Then

            If Me.drpFromCustom.SelectedIndex = -1 Then
                MsgBox("No Start Stage selected", MsgBoxStyle.Exclamation, "Error")
                Me.drpFromCustom.Focus()
                Exit Sub
            End If

            StageStart = Me.drpFromCustom.SelectedValue

        End If

        If Me.radToSold.IsChecked Then
            StageEnd = 99
        ElseIf Me.radToClosed.IsChecked Then
            StageEnd = 999
        ElseIf Me.radToAfterClosed.IsChecked Then
            StageEnd = 0
        ElseIf Me.radToCustom.IsChecked Then

            If Me.drpToCustom.SelectedIndex = -1 Then
                MsgBox("No End Stage selected", MsgBoxStyle.Exclamation, "Error")
                Me.drpToCustom.Focus()
                Exit Sub
            End If

            StageEnd = Me.drpToCustom.SelectedValue

        End If

        If StageEnd <> 0 And StageEnd < StageStart Then
            MsgBox("End Stage Cannot be before Start Stage", MsgBoxStyle.Exclamation, "Error")
            Exit Sub
        End If

        Dim SQL As New StringBuilder
        Dim myTab As DataTable
        Dim myRow As DataRow

        With SQL
            .Append("SELECT I_SL_StockLetterSets.SLS_ID")
            .Append(" FROM I_SL_StockLetterSets LEFT OUTER JOIN")
            .Append("  (SELECT     SLSLink_SLSID")
            .Append(" FROM I_SL_SLSLink")
            .Append(" WHERE      SLSLink_LetterpackID = " & selectedLetterPacks(0) & ") SLSInUse ON I_SL_StockLetterSets.SLS_ID = SLSInUse.SLSLink_SLSID")
            .Append(" Left Outer Join I_SL_SLSTypes on I_SL_StockLetterSets.SLS_TypeID = I_SL_SLSTypes.SLSType_ID")
            .Append(" WHERE (SLSInUse.SLSLink_SLSID Is Null)")
            If Me.radLimitTo.IsChecked Then
                .Append(" AND SLS_TypeID = " & Me.drpLimitTo.SelectedValue)
            End If

        End With

        myTab = CommonDataFunctions.GetSQLDatatable(SQL.ToString)

        If myTab.Rows.Count = 0 Then

            MsgBox("No Stock Letter Sets to add to!", MsgBoxStyle.Exclamation, "Error")
            Exit Sub
        End If

        For Each myRow In myTab.Rows
            Letters.AddSLSLink(myRow.Item("SLS_ID"), selectedLetterPacks(0), 1, Me.ClientType, StageStart, StageEnd)
        Next

        myTab.Dispose()

        Dim resultSLS As DataTable = Letters.SLSList(selectedLetterPacks(0))
        'DataBinding.BindListView(Letters.SLSList(CType(Me.grdLetterPacks.SelectedRows(0), Xceed.Grid.DataRow).Cells(0).Value), Me.lvwSLS)
        DataBinding.BindListView(resultSLS, Me.lvwSLS)
        Dim resultAvailable As DataTable = Letters.SLAAvailableList(selectedLetterPacks(0), Me.SLSType)
        'DataBinding.BindListView(Letters.SLAAvailableList(CType(Me.grdLetterPacks.SelectedRows(0), Xceed.Grid.DataRow).Cells(0).Value, Me.SLSType), Me.lvwAvailable)
        DataBinding.BindListView(resultAvailable, Me.lvwAvailable)

    End Sub

    Private Sub drpLimitTo_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) 'Handles drpLimitTo.SelectedIndexChanged
        Dim selectedLetterPacks As DataRow = Me.grdLetterPacks.SelectedItem
        Dim resultAvailable As DataTable = Letters.SLAAvailableList(selectedLetterPacks(0), Me.SLSType)
        DataBinding.BindListView(resultAvailable, Me.lvwAvailable)
        'DataBinding.BindListView(Letters.SLAAvailableList(CType(Me.grdLetterPacks.SelectedRows(0), Xceed.Grid.DataRow).Cells(0).Value, Me.SLSType), Me.lvwAvailable)
    End Sub

    Private Sub btnAddNewLetterPack_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnAddNewLetterPack.Click

        '        Dim StrModule As String, IntErr As Integer, Ret As Object
        '        StrModule = "Form_FMA_LetterPacks.Btn_AddNew_Click"
        '        Gbl_Str_CodePath = Format(Now(), "ttttt") & " :" & StrModule
        '        On Error GoTo Err_Btn_AddNew_Click
        Dim formCreateLetterPacks As New frmAdminCreateLetterPack
        formCreateLetterPacks.ShowDialog()
        Me.grdLetterPacks.ItemsSource = Letters.LetterPacks


        '        ' Error handling block added by VBA Code Commenter and Error Handler Add-In. DO NOT EDIT this block of code.
        '        Exit Sub

        'Err_Btn_AddNew_Click:
        '        IntErr = GblFunc_Add_Audit("Code Error", StrModule, Err.Number & " : " & Err.Description)
        '        Ret = MsgBox("An error has occured, please contact the IT dept and quote the following code: " & IntErr, , "Error")
        '        Exit Sub
        ' End Error handling block.

    End Sub
End Class
